package org.example.controller;
import org.example.entity.Department;
import org.example.entity.Post;
import org.example.service.DepartmentService;
import org.example.service.JobtransferService;
import org.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Jobtransfer;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/jobtransfer")
public class JobtransferController {
    @Autowired
    private JobtransferService jobtransferService;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PostService postService;
    // 修改 /list 接口
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit) {
        Page<Jobtransfer> pageResult = jobtransferService.getPageWithNames(new Page<>(page, limit));
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", pageResult.getRecords());
        result.put("total", pageResult.getTotal());
        return result;
    }
    // 获取详情接口
    @GetMapping("/detail/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Jobtransfer jobtransfer = jobtransferService.getById(id);
            if (jobtransfer != null) {
                result.put("code", 200);
                result.put("data", jobtransfer);
                result.put("message", "查询成功");
            } else {
                result.put("code", 404);
                result.put("message", "未找到该记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    // 更新接口
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody Jobtransfer jobtransfer) {
        Map<String, Object> result = new HashMap<>();
        try {
            System.out.println("接收到的更新数据: " + jobtransfer);
            System.out.println("接收到的 employeeId: " + jobtransfer.getEmployeeId());

            // 检查ID是否存在
            if (jobtransfer.getEmployeeId() == null) {
                result.put("code", 400);
                result.put("message", "员工ID不能为空");
                return result;
            }

            Jobtransfer existing = jobtransferService.getById(jobtransfer.getEmployeeId());
            if (existing == null) {
                result.put("code", 404);
                result.put("message", "要更新的记录不存在，ID: " + jobtransfer.getEmployeeId());
                return result;
            }

            jobtransfer.setUpdateTime(new java.util.Date());

            boolean updated = jobtransferService.updateById(jobtransfer);
            if (updated) {
                result.put("code", 200);
                result.put("message", "更新成功");
            } else {
                result.put("code", 500);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "更新失败: " + e.getMessage());
        }
        return result;
    }
    // 删除接口
    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();
        try {
            @SuppressWarnings("unchecked")
            List<Integer> ids = (List<Integer>) request.get("ids");
            if (ids != null && !ids.isEmpty()) {
                boolean removed = jobtransferService.removeByIds(ids);
                if (removed) {
                    result.put("code", 200);
                    result.put("message", "删除成功");
                } else {
                    result.put("code", 500);
                    result.put("message", "删除失败");
                }
            } else {
                result.put("code", 400);
                result.put("message", "请选择要删除的记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }

    // 新增接口
// 在 JobtransferController.java 中
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody Jobtransfer jobtransfer) {
        Map<String, Object> result = new HashMap<>();
        try {
            jobtransfer.setEmployeeId(null);

            // 设置其他字段
            jobtransfer.setSubmitTime(new Date());
            jobtransfer.setUpdateTime(new Date());

            boolean saved = jobtransferService.save(jobtransfer);
            if (saved) {
                result.put("code", 200);
                result.put("message", "添加成功");
            } else {
                result.put("code", 500);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "添加失败: " + e.getMessage());
        }
        return result;
    }
}